package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-10
 * Time: 12:56
 */
public class Delete implements Ioperation {
    @Override
    public void work(BookList bookList) {
        int pos = -1;
        System.out.println("删除图书");
        System.out.println("请输入要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int i = 0;
        for (; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(bookName)) {
                pos = i;
                break;
            }
        }
        //
        if (i >= bookList.getUsedSize()) {
            System.out.println("没找到你要删除的书");
            return;
        }
        //开始删除操作
        for (int j = pos; j < bookList.getUsedSize() - 1; j++) {
            Book book = bookList.getBook(j + 1);//把j+1下标的书先拿出来
            bookList.setBook(j, book);//j+1的书(book)给j
        }
        bookList.setUsedSize(bookList.getUsedSize() - 1);
        bookList.setBook(bookList.getUsedSize(), null);//删除最后一个位置的书
        System.out.println("删除成功!!");
    }
}
